import Vue from "vue";
import VueRouter from "vue-router";
import findLast from "lodash/findLast";
import { notification } from "ant-design-vue";
import NProgress from "nprogress";
import "nprogress/nprogress.css";
// import BlankLayout from "../layouts/BlankLayout.vue";
import NotFound from "@/views/404";
import Forbidden from "@/views/403";
import { check, isLogin } from "@/utils/auth";

Vue.use(VueRouter);

// hideInMenu         是否在菜单栏显示当前菜单
// hideChildrenInMenu 是否在菜单栏显示当前菜单的子集菜单

const routes = [
  {
    path: "/user",
    hideInMenu: true,
    // component: BlankLayout,
    // component: { render: (h) => h("router-view") },
    component: () =>
      import(/* webpackChunkName: "userLayout" */ "@/layouts/UserLayout.vue"),
    children: [
      {
        path: "/user",
        redirect: "/user/login",
      },
      {
        path: "/user/login",
        name: "login",
        component: () =>
          import(/* webpackChunkName: "user" */ "@/views/User/Login.vue"),
      },
    ],
  },
  {
    path: "/",
    meta: { authority: ["user", "admin"] },
    component: () =>
      import(/* webpackChunkName: "basicLayout" */ "@/layouts/BasicLayout.vue"),
    children: [
      // dashboard
      {
        path: "/",
        redirect: "/home",
      },
      {
        path: "/home",
        name: "home",
        meta: { icon: "home", title: "首页" },
        component: () =>
          import(/* webpackChunkName: "home" */ "@/views/home/index.vue"),
      },
      // form
      {
        path: "/supermarket",
        name: "supermarket",
        meta: { icon: "bank", title: "超市管理", authority: ["admin"] },
        component: { render: (h) => h("router-view") },
        children: [
          {
            path: "/supermarket",
            redirect: "/supermarket/list",
          },
          {
            path: "/supermarket/list",
            name: "list",
            meta: { title: "超市列表" },
            component: () =>
              import(
                /* webpackChunkName: "supermarket" */ "@/views/supermarket/list"
              ),
          },
          {
            path: "/supermarket/cash",
            name: "cash",
            meta: { title: "收银机管理" },
            hideChildrenInMenu: true,
            component: () =>
              import(
                /* webpackChunkName: "supermarket" */ "@/views/supermarket/cash"
              ),
          },
          {
            path: "/supermarket/cashier",
            name: "cashier",
            meta: { title: "收银员管理" },
            hideChildrenInMenu: true,
            component: () =>
              import(
                /* webpackChunkName: "supermarket" */ "@/views/supermarket/cashier"
              ),
          },
        ],
      },
      {
        path: "/product",
        name: "product",
        meta: { icon: "appstore", title: "商品分类", authority: ["admin"] },
        component: { render: (h) => h("router-view") },
        children: [
          {
            path: "/product",
            redirect: "/product/type-list",
          },
          {
            path: "/product/type-list",
            name: "type-list",
            meta: { title: "分类列表" },
            component: () =>
              import(
                /* webpackChunkName: "product" */ "@/views/product/typeList"
              ),
          },
          {
            path: "/product/product-list",
            name: "product-list",
            meta: { title: "商品列表" },
            hideChildrenInMenu: true,
            component: () =>
              import(
                /* webpackChunkName: "product" */ "@/views/product/productList"
              ),
          },
        ],
      },
      {
        path: "/stat",
        name: "stat",
        meta: {
          icon: "pie-chart",
          title: "统计分析查询",
          authority: ["admin"],
        },
        component: { render: (h) => h("router-view") },
        children: [
          {
            path: "/stat",
            redirect: "/stat/supermarket",
          },
          {
            path: "/stat/supermarket",
            name: "supermarket",
            meta: { title: "超市汇总" },
            component: { render: (h) => h("router-view") },
            hideChildrenInMenu: true,
            children: [
              {
                path: "/stat/supermarket",
                redirect: "/stat/supermarket/index",
              },
              {
                path: "/stat/supermarket/index",
                name: "market",
                component: () =>
                  import(
                    /* webpackChunkName: "stat" */ "@/views/stat/supermarket"
                  ),
              },
              {
                path: "/stat/supermarket/detail",
                name: "detail",
                meta: { title: "收银机明细" },
                component: () =>
                  import(
                    /* webpackChunkName: "stat" */ "@/views/stat/supermarket/detail"
                  ),
              },
            ],
          },

          {
            path: "/stat/order",
            name: "order",
            meta: { title: "商品订单列表" },
            // hideChildrenInMenu: true,
            component: () =>
              import(/* webpackChunkName: "stat" */ "@/views/stat/order"),
          },
          {
            path: "/stat/abnormal",
            name: "abnormal",
            meta: { title: "识别异常订单" },
            // hideChildrenInMenu: true,
            component: () =>
              import(/* webpackChunkName: "stat" */ "@/views/stat/abnormal"),
          },
          {
            path: "/stat/turnover",
            name: "turnover",
            meta: { title: "分类总营业额" },
            // hideChildrenInMenu: true,
            component: () =>
              import(/* webpackChunkName: "stat" */ "@/views/stat/turnover"),
          },
          {
            path: "/stat/cash",
            name: "cash",
            meta: { title: "收银机汇总" },
            // hideChildrenInMenu: true,
            component: () =>
              import(/* webpackChunkName: "stat" */ "@/views/stat/cash"),
          },
          {
            path: "/stat/sales",
            name: "sales",
            meta: { title: "商品销售统计" },
            // hideChildrenInMenu: true,
            component: () =>
              import(/* webpackChunkName: "stat" */ "@/views/stat/sales"),
          },
        ],
      },
      {
        path: "/analysis",
        name: "analysis",
        meta: { icon: "fund", title: "数据分析", authority: ["admin"] },
        component: { render: (h) => h("router-view") },
        children: [
          {
            path: "/analysis",
            redirect: "/analysis/operating",
          },
          {
            path: "/analysis/operating",
            name: "operating",
            meta: { title: "经营分析" },
            component: () =>
              import(
                /* webpackChunkName: "analysis" */ "@/views/analysis/operating"
              ),
          },
        ],
      },
      {
        path: "/abnormal",
        name: "abnormal",
        meta: { icon: "alert", title: "异常预警", authority: ["admin"] },
        component: { render: (h) => h("router-view") },
        children: [
          {
            path: "/abnormal",
            redirect: "/abnormal/offline",
          },
          {
            path: "/abnormal/offline",
            name: "offline",
            meta: { title: "频繁离线预警" },
            component: () =>
              import(
                /* webpackChunkName: "abnormal" */ "@/views/abnormal/offline"
              ),
          },
          {
            path: "/abnormal/not-online",
            name: "not-online",
            meta: { title: "七天未上线预警" },
            component: () =>
              import(
                /* webpackChunkName: "abnormal" */ "@/views/abnormal/notOnline"
              ),
          },
          {
            path: "/abnormal/transaction",
            name: "transaction",
            meta: { title: "七天交易异常预警" },
            component: () =>
              import(
                /* webpackChunkName: "abnormal" */ "@/views/abnormal/transaction"
              ),
          },
          {
            path: "/abnormal/statistics",
            name: "statistics",
            meta: { title: "预警信息统计" },
            component: () =>
              import(
                /* webpackChunkName: "abnormal" */ "@/views/abnormal/statistics"
              ),
          },
        ],
      },
      {
        path: "/log",
        name: "log",
        meta: { icon: "clock-circle", title: "操作日志", authority: ["admin"] },
        component: () =>
          import(/* webpackChunkName: "log" */ "@/views/log/index.vue"),
      },
      {
        path: "/system",
        name: "system",
        meta: { icon: "setting", title: "系统设置", authority: ["admin"] },
        component: { render: (h) => h("router-view") },
        children: [
          {
            path: "/system",
            redirect: "/system/mechanism",
          },
          {
            path: "/system/mechanism",
            name: "mechanism",
            meta: { title: "税务机构管理" },
            component: () =>
              import(
                /* webpackChunkName: "system" */ "@/views/system/mechanism"
              ),
          },
          {
            path: "/system/type",
            name: "type",
            meta: { title: "注册类型管理" },
            component: () =>
              import(/* webpackChunkName: "system" */ "@/views/system/type"),
          },
          {
            path: "/system/data",
            name: "data",
            meta: { title: "集团数据管理" },
            component: () =>
              import(/* webpackChunkName: "system" */ "@/views/system/data"),
          },
        ],
      },
    ],
  },
  {
    path: "/403",
    name: "403",
    hideInMenu: true,
    component: Forbidden,
  },
  {
    path: "*",
    name: "404",
    hideInMenu: true,
    component: NotFound,
  },
];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes,
});

router.beforeEach((to, from, next) => {
  if (to.path !== from.path) {
    NProgress.start();
  }
  const record = findLast(to.matched, (record) => record.meta.authority);
  if (record && !check(record.meta.authority)) {
    if (!isLogin() && to.path !== "/user/login") {
      next({
        path: "/user/login",
      });
    } else if (to.path !== "/403") {
      notification.error({
        message: "403",
        description: "你没有权限访问，请联系管理员咨询。",
      });
      next({
        path: "/403",
      });
    }
    NProgress.done();
  }

  next();
});

router.afterEach(() => {
  NProgress.done();
});

export default router;
